unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, RzButton, Mask, RzEdit, rtcInfo, rtcConn, rtcTcpSrv,
  rtcTcpCli;

type
  TKzTcpClient=class(TRtcTcpClient)
  public
    SourceAddr:string;
    SourcePort:string;
    TcpConnect:TRtcConnection;
    IsConnect :Boolean;
    ReadedTxt :string;
  public  
    procedure OnClientDataReceived(Sender: TRtcConnection);
    procedure OnClientDisconnect(Sender: TRtcConnection);
    procedure OnClientConnectError(Sender: TRtcConnection; E: Exception);
    procedure OnClientConnectFail(Sender: TRtcConnection);
  end;

  TForm1 = class(TForm)
    Edit_1: TRzEdit;
    Edit_2: TRzEdit;
    Edit_3: TRzEdit;
    Edit_4: TRzEdit;
    Btnx_1: TRzButton;
    Labl_1: TLabel;
    Labl_3: TLabel;
    Labl_2: TLabel;
    Labl_4: TLabel;
    TcpServer_1: TRtcTcpServer;
    Client_1: TRtcTcpClient;
    Memo_1: TRzMemo;
    Btnx_2: TButton;
    procedure Btnx_1Click(Sender: TObject);
    procedure TcpServer_1ListenStart(Sender: TRtcConnection);
    procedure FormDestroy(Sender: TObject);
    procedure TcpServer_1DataReceived(Sender: TRtcConnection);
    procedure FormShow(Sender: TObject);
    procedure Btnx_2Click(Sender: TObject);
  private

  public
  end;

var
  Form1: TForm1;
  FStrsDeal:TStringList;  

implementation

uses
  Class_KzUtils;

{$R *.dfm}

procedure TForm1.Btnx_1Click(Sender: TObject);
begin
  TcpServer_1.ServerAddr:=Trim(Edit_3.Text);
  TcpServer_1.ServerPort:=Trim(Edit_4.Text);
  TcpServer_1.MultiThreaded:=True;
  TcpServer_1.Listen(True);
end;

procedure TForm1.TcpServer_1ListenStart(Sender: TRtcConnection);
begin
  Caption:='Listen Start';
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  if TcpServer_1.isListening then
  begin
    TcpServer_1.StopListen;
  end;

  if FStrsDeal<>nil then
  begin
    TKzUtils.TryFreeAndNil(FStrsDeal);
  end;  
end;

procedure TForm1.TcpServer_1DataReceived(Sender: TRtcConnection);
var
  ReadA:string;
  TcpClient:TKzTcpClient;
begin
  if Sender.inMainThread then
  begin
    ReadA:=Sender.Read;
    //Memo_1.Lines.Add(Format('recv:%S:%S:%S',[ReadA,Sender.PeerAddr,Sender.PeerPort]));

    TcpClient:=TKzTcpClient.Create(nil);
    TcpClient.SourceAddr:=Sender.PeerAddr;
    TcpClient.SourcePort:=Sender.PeerPort;
    TcpClient.TcpConnect:=Sender;
    
    TcpClient.ServerAddr:=Trim(Edit_1.Text);
    TcpClient.ServerPort:=Trim(Edit_2.Text);
    //TcpClient.OnDataReceived:=TcpClient.Client_1DataReceived;
    TcpClient.OnDisconnect  :=TcpClient.OnClientDisconnect;
    TcpClient.OnConnectError:=TcpClient.OnClientConnectError;
    TcpClient.OnConnectFail :=TcpClient.OnClientConnectFail;
    TcpClient.Connect();
    TcpClient.Write(ReadA);

    FStrsDeal.AddObject(Format('%S-%S',[TcpClient.SourceAddr,TcpClient.SourcePort]),TcpClient);

    Caption:=Format('Client:%D',[FStrsDeal.Count]);
  end else
  begin
    Sender.Sync(TcpServer_1DataReceived);
  end;    
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  FStrsDeal:=TStringList.Create;
end;


{ TOneDeal }


{ TKzTcpClient }




procedure TKzTcpClient.OnClientConnectError(Sender: TRtcConnection;
  E: Exception);
begin
  TcpConnect.Write('FAILBANK');
end;

procedure TKzTcpClient.OnClientConnectFail(Sender: TRtcConnection);
begin
  TcpConnect.Write('FAILBANK');
end;

procedure TKzTcpClient.OnClientDataReceived(Sender: TRtcConnection);
begin

end;

procedure TKzTcpClient.OnClientDisconnect(Sender: TRtcConnection);
var
  IdexA:Integer;
  TcpClient:TKzTcpClient;
begin
  ReadedTxt:=Sender.Read;
  TcpConnect.Write(ReadedTxt);
  TcpConnect.Disconnect;


  IdexA:=FStrsDeal.IndexOf(Format('%S-%S',[SourceAddr,SourcePort]));
  if IdexA<>-1 then
  begin
    {TcpClient:=TKzTcpClient(FStrsDeal.Objects[IdexA]);
    if TcpClient<>nil then
    begin
      FStrsDeal.Delete(IdexA);
      if TcpClient.IsConnect then
      begin
        TcpClient.Disconnect;
      end;
      TcpClient.Free;
      FreeAndNil(TcpClient);
    end;}
  end;
end;

procedure TForm1.Btnx_2Click(Sender: TObject);
begin
  if FStrsDeal<>nil then
  begin
    TKzUtils.TryFreeAndNil(FStrsDeal);
  end;
  FStrsDeal:=nil;
  FStrsDeal:=TStringList.Create;
  
end;

end.
